package com.yanggu.payment.controller;

import com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders;
import com.yanggu.payment.entity.weixinpay.resp.WechatAppQueryResult;
import com.yanggu.payment.entity.weixinpay.resp.WeiXinPayApplyRefundResp;
import com.yanggu.payment.service.WeiXinPayService;
import com.yanggu.payment.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2022/5/4 23:27
 * @description:
 */
@RestController
@RequestMapping("/api/wx-pay")
@Api(tags = "网站微信支付API")
public class WeiXinPayController {

    @Autowired
    private WeiXinPayService weiXinPayService;

    @ApiOperation("统一下单API, 生成订单, 返回支付二维码")
    @PostMapping("/native/{productId}")
    public R nativePay(@PathVariable("productId") Long productId) {
        //返回支付二维码连接和订单号
        Map<String, Object> map = weiXinPayService.nativePay(productId);
        return R.ok().setData(map);
    }

    @ApiOperation("微信支付通知接口")
    @PostMapping("/native/notify")
    public ResponseEntity<R> nativeNotify(@RequestBody String body,
                                          @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_SERIAL) String serialNumber,
                                          @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_NONCE) String nonce,
                                          @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_TIMESTAMP) String timestamp,
                                          @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_SIGNATURE) String signature) throws Exception {
        R r = weiXinPayService.nativeNotify(body, serialNumber, nonce, timestamp, signature);
        return ResponseEntity.ok(r);
        //接收失败：HTTP应答状态码需返回5XX或4XX，同时需返回应答报文。微信支付服务器会进行重试
        //return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }

    /**
     * 取消订单
     *
     * @param orderNo
     * @return
     */
    @ApiOperation("取消订单")
    @PostMapping("/cancel/{orderNo}")
    public R cancelOrder(@PathVariable("orderNo") String orderNo) {
        weiXinPayService.cancelOrder(orderNo);
        return R.ok();
    }

    /**
     * 申请退款
     *
     * @param orderNo
     * @param reason
     * @return
     */
    @ApiOperation("申请退款")
    @PostMapping("/refunds/{orderNo}/{reason}")
    public R refunds(@PathVariable("orderNo") String orderNo,
                     @PathVariable("reason") String reason) {
        weiXinPayService.refunds(orderNo, reason);
        return R.ok();
    }

    /**
     * 退款结果通知
     *
     * @return
     */
    @ApiOperation("退款结果通知")
    @PostMapping("/refunds/notify")
    public ResponseEntity<R> refundsNotify(@RequestBody String body,
                                           @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_SERIAL) String serialNumber,
                                           @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_NONCE) String nonce,
                                           @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_TIMESTAMP) String timestamp,
                                           @RequestHeader(WechatPayHttpHeaders.WECHAT_PAY_SIGNATURE) String signature) throws Exception {
        R r = weiXinPayService.refundsNotify(body, serialNumber, nonce, timestamp, signature);
        return ResponseEntity.ok(r);
    }

    @ApiOperation("下载交易订单或者资金订单")
    @GetMapping("/downloadbill/{billDate}/{type}")
    public R downloadBill(@PathVariable("billDate") String billDate,
                          @PathVariable("type") String type) {
        String result = weiXinPayService.downloadBill(billDate, type);
        return R.ok().data("result", result);
    }

    @ApiOperation("微信查询支付订单接口(测试使用)")
    @GetMapping("/query/{orderNo}")
    public R queryOrder(@PathVariable("orderNo") String orderNo) {
        WechatAppQueryResult result = weiXinPayService.queryOrder(orderNo);
        return R.ok().data("result", result);
    }

    @ApiOperation("微信查询退款订单接口(测试使用)")
    @GetMapping("/query/refunds/{refundNo}")
    public R queryRefundNo(@PathVariable("refundNo") String refundNo) {
        WeiXinPayApplyRefundResp data = weiXinPayService.queryRefundNo(refundNo);
        return R.ok().data("result", data);
    }

}
